/**
 * NPC
 * author:noodles 
 */
package  com.gamebase.role
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import com.gamebase.role.vo.AvatarEventConst;

	public class NPC extends Role
	{
		public var roleMode:Avatar;
		private var loader:Loader;
		private var headProcess:MovieClip;//头顶进度动画
		
		public function NPC()
		{
			super();
			roleMode = new Avatar();
			roleMode.addEventListener(AvatarEventConst.AVATAR_LOADED,modeInitalize);
			this.addChild(roleMode);
			this.mouseChildren = false;
		}
		
		
		private function modeInitalize(evt:Event):void
		{
			resetFront(
			new Rectangle(0,0,roleMode.width,roleMode.height),
			new Point(- roleMode.width/2 , - roleMode.realBottom)
			)
		}
		
		/**更改角色身体部位
		 *  @param changePartList:结构为[part:String,partURL:String]，part值为："all","head","body","leg","feet"
		 * */
		public function changPart(changePartList:Array):void
		{
			if(this.parent == null)
			{
				trace("换装失败，请先将角色添加到显示列表再换装");
				return;
			}
			roleMode.changBodyPart(changePartList);
		}
		
		/**开始移动 
		 * @parma movePath:移动路径 
		 * @parma cb：移动到目的地后要执行的函数
		 * */
		public function beginMove(movePath:Array,cb:Function=null,moveType:String="walk"):void
		{
			roleMode.beginMove(movePath,cb,moveType);
		}
		
		/**添加头顶进度动画
		 * @param movieURL指定的进度动画必须包含reSet()和setPercent(p:uint)方法
		 * */
		public function addHeadProcess(movieURL:String=null):void
		{
			if(movieURL == null) movieURL = "assets/swf/process/HeadProcess.swf";
			if(loader == null)
			{
				loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,headProcessLoaded);
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loadError);
			}
			loader.load(new URLRequest(movieURL));
		}
		
		private function loadError(evt:IOErrorEvent):void
		{
			trace(evt.text);
		}
		
		private function headProcessLoaded(evt:Event):void
		{
			var loadInfo:LoaderInfo = evt.target as LoaderInfo;
			headProcess = loadInfo.content as MovieClip;
		}
		
		/**设置头顶进度条进度
		 * */
		public function setHeadProcess(p:uint):void
		{
			if(headProcess != null)
			{
				if(!this.contains(headProcess))
				{
					headProcess.x = -(headProcess.width)/2;
					headProcess.y = (roleMode.realTop - roleMode.realBottom)-8;
					this.addChild(headProcess);
				}
				headProcess.setPercent(p);
				if(p < 1)
				{
					this.removeChild(headProcess);
				}
			}
		}
		
		/**移除头顶进度条，注意：调用setHeadProcess方法会自动再将进度条加上*/
		public function removeHeadProcess():void
		{
			if(headProcess != null)
			{
				if(this.contains(headProcess))
				{
					this.removeChild(headProcess);
				}
			}
		}
		
		//是否包含于角色的非空像素
		public function containPoint(_x:Number,_y:Number):Boolean
		{
			return roleMode.containPoint(_x,_y);
		}
				
		public function destroy():void
		{
			if(loader != null)
			{
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,headProcessLoaded);
				loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,loadError);
				loader = null;
			}
			roleMode.removeEventListener("avatarLoaded",modeInitalize);
			roleMode.destroy();
		}
		
	}
}